<template>
    <div class="role-wrap" style="padding: 6px">
        <el-card>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <!-- <el-form-item label="车辆属性" prop="carCode">
                    <el-select v-model="queryParams.carCode" placeholder="车辆属性" clearable style="width: 240px">
                        <el-option label="单位" value="单位" />
                        <el-option label="个人" value="个人" />
                    </el-select>
                </el-form-item>
                <el-form-item label="车牌" prop="carPlate">
                    <el-input v-model="queryParams.carPlate" placeholder="请输入车牌" clearable style="width: 240px"
                        @keyup.enter.native="handleQuery" />
                </el-form-item> -->

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>

            </el-form>
        </el-card>
        <el-card class="main-card">
            <!-- <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    <el-button type="success" icon="el-icon-upload" size="mini" @click="openImport">导入</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row> -->
            <el-table class="base-table" v-loading="loading" :data="roleList">
                <el-table-column label="系统版本" prop="version" width="200" />
                <el-table-column label="基本配置" prop="base" width="300"  />
                <el-table-column label="串口配置" prop="uconf" width="200"  />
                <el-table-column label="网络配置" prop="nconf" align="center" width="100">
             
                </el-table-column>
                <el-table-column label="modbus配置" align="center" prop="mb" width="200">

                </el-table-column>
                <el-table-column label="参数版本号" prop="param_ver" width="200"  />
                 <el-table-column label="用户参数" prop="up" width="200"  />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" plain style="padding: 5px" icon="el-icon-edit"
                            @click="handleUpdate(scope.row)">编辑</el-button>
                      
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

    </div>
</template>

<script>
import {  queryPageconfig } from "./api/sysconfig.js";
import axios from 'axios';
import { getToken } from '@/utils/auth'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    dicts: ['sys_normal_disable'],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            dialogVisible: false,

            // 总条数
            total: 0,
            // 角色表格数据
            roleList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,


            // 查询参数
            queryParams: {
            
                pageNum: 1,
                pageSize: 10,
             
            },
            // 表单参数
            form: {},

            // 表单校验
            rules: {
                carCode: [
                    { required: true, message: "车辆属性不能为空", trigger: "change" }
                ],
                whitelistIp: [
                    { required: true, message: "白名单不能为空", trigger: "blur" }
                ],
                carPlate: [
                    { required: true, message: "车牌号不能为空", trigger: "blur" }
                ],
                cmpName: [
                    { required: true, message: "归属单位/个人名称不能为空", trigger: "blur" }
                ],
                carType: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                status: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                start: [
                    { required: true, message: "车辆通行开始时间不能为空", trigger: "change" }
                ],
                end: [
                    { required: true, message: "车辆通行结束时间不能为空", trigger: "change" }
                ],
                //start
            },
            deviceList: []
        };
    },
    created() {
        this.getList();
    },
    methods: {
     
        //下载模板
        handleImport() {
            let obj = {
                fileName: "车辆信息上传模板-车牌.xlsx"
            }
            // downloadCarTempate(obj).then(res => {
            //     console.log(res);

            // })
            this.download2('system/car_details/template/downLoad', {obj}, `车辆信息上传模板-车牌.xlsx`)
            // axios.get('/dev-api/system/car_details/template/downLoad', {
            //     params: {
            //         fileName: '车辆信息上传模板-车牌.xlsx'
            //     },
            //     headers: {
            //         "Authorization": 'Bearer ' + getToken(),
            //         "Content-type":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
            //     },

            // }, { responseType: 'blob' }).then(res => {
            //     console.log(res)
            // })


            axios.get('/dev-api/system/car_details/template/downLoad', {
                params: {
                    fileName: "车辆信息上传模板-车牌.xlsx",
                },
                headers: {
                    'Authorization': 'Bearer ' + getToken()
                },
            }, { responseType: 'blob' })
                .then((response) => {
                    console.log(response);

                    const url = window.URL.createObjectURL(new Blob([response.data]));
                    const link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', '车辆信息上传模板-车牌.xlsx');
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                })
                .catch((error) => {
                    console.error(error);
                });
            // this.download2('system/car_details/template/downLoad',obj, `车辆信息上传模板-车牌.xlsx`)
        },
        /** 查询角色列表 */
        getList() {
            this.loading = true;
            queryPageconfig(this.queryParams).then(response => {       
                this.roleList = response.rows;
                this.total = response.total;
                this.loading = false;
            }
            );
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        // 表单重置
        reset() {
            this.form = {}
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {

            this.resetForm("queryForm");
            this.handleQuery();
        },

        /** 新增按钮操作 */
        handleAdd() {
            // this.reset();
            this.open = true;
            this.title = "添加";
            this.isEdit = true;
            this.getWhiteCarListFun()

        },

        getWhiteCarListFun() {
            getWhiteCarList().then((res) => {
                if (res.code == 0) {
                    this.deviceList = res.data
                }

            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            console.log(row);
            this.getWhiteCarListFun()
            row.status = (row.status).toString()
            row.whitelistIp = row.whitelistIp.split(",")
            this.reset();
            this.form = row;
            this.open = true;
            this.title = "编辑";

        },

        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.whitelistIp = (this.form.whitelistIp).toString()
                    if (this.form.id) {

                        editCar(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {

                        addCar(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 提交按钮（数据权限） */
        submitDataScope: function () {
            if (this.form.roleId != 0) {
                this.form.deptIds = this.getDeptAllCheckedKeys();
                dataScope(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.openDataScope = false;
                    this.getList();
                });
            }
        },
        /** 删除按钮操作 */
        handleDelete(row) {

            this.$modal.confirm('是否确认删除?').then(function () {
                return deleteCar(row.id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/role/export', {
                ...this.queryParams
            }, `role_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style>
.role-wrap {
    padding: 6px;
}

.top-card-wrap {
    margin-bottom: 5px;
}

.card-wrap {
    padding-bottom: 100px;
}
</style>